package cn.lyjuan.android.demofive.frame.databinding;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.jimi_wu.ptlrecyclerview.Divider.BaseItemDecoration;
import com.jimi_wu.ptlrecyclerview.PullToLoad.OnLoadListener;
import com.jimi_wu.ptlrecyclerview.PullToRefresh.OnRefreshListener;

import java.util.ArrayList;

import cn.lyjuan.android.demofive.R;
import cn.lyjuan.android.demofive.databinding.FrameDatabindingRecyclerActivityBinding;
import cn.lyjuan.android.demofive.databinding.FrameDatabindingRecyclerItemBinding;

/**
 * Created by chad on 06/11/2017.
 */

public class RecyclerListDatabindingActivity extends AppCompatActivity
{
    private FrameDatabindingRecyclerActivityBinding binding;
    private ArrayList<UserView> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.frame_databinding_recycler_activity);

        setView();

        loadData();
    }

    protected void setView()
    {
//        binding.recyclerView.setLayoutManager(new PTLLinearLayoutManager(PTLLinearLayoutManager.VERTICAL));
        binding.recyclerView.addItemDecoration(new BaseItemDecoration(this, R.drawable.shape_recycler_divider, 2, 1));
        binding.recyclerView.setAdapter(new RecyclerView.Adapter<DataBindViewHolder>()
        {
            @Override
            public DataBindViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
            {
                FrameDatabindingRecyclerItemBinding binding = DataBindingUtil.inflate(RecyclerListDatabindingActivity.this.getLayoutInflater(),
                        R.layout.frame_databinding_recycler_item, parent, false);
                return new DataBindViewHolder(binding);
            }

            @Override
            public void onBindViewHolder(final DataBindViewHolder holder, final int position)
            {
                final UserView user = list.get(position);
                holder.binding.setUser(user);
                holder.binding.setClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Snackbar.make(holder.binding.getRoot(), "click >> " + user.getName(), Snackbar.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public int getItemCount()
            {
                return list.size();
            }
        });
        binding.recyclerView.setOnLoadListener(new OnLoadListener()
        {
            @Override
            public void onStartLoading(int i)
            {
                Snackbar.make(binding.recyclerView, "loading start >> " + i, Snackbar.LENGTH_SHORT).show();

                handler.postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        int count = loadData();
                        binding.recyclerView.completeLoad(count);
                    }
                }, 2000);
            }
        });

        binding.recyclerView.setOnRefreshListener(new OnRefreshListener()
        {
            @Override
            public void onStartRefreshing()
            {
                handler.postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        int count = loadData();
                        binding.recyclerView.completeRefresh();
                    }
                }, 2000);
            }
        });
    }

    private Handler handler = new Handler();

    protected int loadData()
    {
        int count = 10;
        UserView user = null;
        for (int i = 0; i < count; i++)
        {
            user = new UserView();
            user.setAge(String.valueOf(i));
            user.setName("Name-" + i);

            list.add(user);
        }

        return count;
    }

    public class DataBindViewHolder extends RecyclerView.ViewHolder
    {
        private FrameDatabindingRecyclerItemBinding binding;

        public DataBindViewHolder(final FrameDatabindingRecyclerItemBinding binding)
        {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
